﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XingLucifer.Devices.Models;
using XingLucifer.IBase;

namespace XingLucifer.Devices.Devices.RFID
{
    public class RD900M : IDevices
    {
        private ICommunication _com;
        private CancellationTokenSource _tokenSource;
        public RD900M(ICommunication com, CancellationTokenSource tokenSource)
        {
            _com = com;
            _tokenSource = tokenSource;
        }

        public bool WriteClass<TClass>(TClass value, int address, int count = 3) where TClass : class
        {
            throw new NotImplementedException("未实现");
        }

        public TClass ReadClass<TClass>(int address, TClass obj = null, int count = 3) where TClass : class
        {
            throw new NotImplementedException("未实现");
        }

        public Task Read()
        {
            _ = Task.Run(() =>
            {
                while (!_tokenSource.Token.IsCancellationRequested)
                {
                    byte[] bytes = default;
                    try
                    {
                        _com.Write(_cmd);
                        Thread.Sleep(400);
                        bytes = _com.Read(64);
                        switch (bytes.Length)
                        {
                            case 6:
                                _barcode = bytes[3].ToString("X");
                                continue;
                            case 20:
                                StringBuilder sb = new StringBuilder();
                                foreach (byte b in bytes.Skip(12).Take(6).ToArray())
                                {
                                    sb.Append(b.ToString("X2"));
                                }
                                _barcode = sb.ToString().ToUpper();
                                Thread.Sleep(3000);
                                continue;
                        }
                        _barcode =  default;
                    }
                    catch (Exception ex)
                    {
                        if (!_tokenSource.Token.IsCancellationRequested)
                        {
                            _com.ReconnectDisconnection?.Invoke(_com, _com.Index, _com.IP, _com.Port, _com.DeviceType, _com.TaskType, _com.ServiceName, _tokenSource);
                        }
                        _barcode = default;
                    }
                    Thread.Sleep(200);
                }
            });
            return Task.CompletedTask;
        }

        private string _barcode = default;

        public bool WriteSingle(object value, int address, int offset, int length, int count = 3)
        {
            throw new NotImplementedException("未实现");
        }

        private byte[] _cmd = new byte[] { 0x04, 0x00, 0x01, 0xDB, 0x4B };
        public TValue? ReadSingle<TValue>(int address, int length, int count = 3)
        {
            return (TValue)(object)_barcode;
        }

        public bool Open() => _com.Open();

        public void Close() => _com.Close();
    }
}
